package cn.iocoder.yudao.module.im.dal.mysql.member;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.im.controller.app.member.vo.AppMemberMessagePageReqVO;
import cn.iocoder.yudao.module.im.dal.dataobject.member.MemberMessageDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.im.controller.admin.member.vo.*;

/**
 * IM消息 Mapper
 *
 * @author Jerry
 */
@Mapper
public interface MemberMessageMapper extends BaseMapperX<MemberMessageDO> {

    default PageResult<MemberMessageDO> selectPage(MemberMessagePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberMessageDO>()
                .eqIfPresent(MemberMessageDO::getGroupId, reqVO.getGroupId())
                .eqIfPresent(MemberMessageDO::getSendUserId, reqVO.getSendUserId())
                .eqIfPresent(MemberMessageDO::getReceiveUserId, reqVO.getReceiveUserId())
                .eqIfPresent(MemberMessageDO::getContentType, reqVO.getContentType())
                .eqIfPresent(MemberMessageDO::getContent, reqVO.getContent())
                .eqIfPresent(MemberMessageDO::getVoiceSec, reqVO.getVoiceSec())
                .eqIfPresent(MemberMessageDO::getImgWidth, reqVO.getImgWidth())
                .eqIfPresent(MemberMessageDO::getImgHeight, reqVO.getImgHeight())
                .eqIfPresent(MemberMessageDO::getReadStatus, reqVO.getReadStatus())
                .betweenIfPresent(MemberMessageDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MemberMessageDO::getId));
    }

    default PageResult<MemberMessageDO> selectPage(AppMemberMessagePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MemberMessageDO>()
                .eqIfPresent(MemberMessageDO::getGroupId, reqVO.getGroupId())
                .orderByDesc(MemberMessageDO::getId));
    }

    default Long UnreadMessageCount(Long groupId, Long userId){
        return selectCount(new LambdaQueryWrapperX<MemberMessageDO>()
                .eqIfPresent(MemberMessageDO::getReadStatus, false)
                .eqIfPresent(MemberMessageDO::getGroupId, groupId)
                .eqIfPresent(MemberMessageDO::getReceiveUserId, userId));
    }

    default void updateMessageRead(Long groupId, Long userId){
        LambdaUpdateWrapper<MemberMessageDO> updateWrapper = new LambdaUpdateWrapper<MemberMessageDO>()
                .set(MemberMessageDO::getReadStatus, true)
                .eq(MemberMessageDO::getSendUserId, userId)
                .eq(MemberMessageDO::getReadStatus, false)
                .eq(MemberMessageDO::getGroupId, groupId);
        update(null, updateWrapper);
    }

    default MemberMessageDO getLastMessage(Long groupId){
        return selectOne(new LambdaQueryWrapperX<MemberMessageDO>()
                .eqIfPresent(MemberMessageDO::getGroupId, groupId)
                .orderByDesc(MemberMessageDO::getId).last("LIMIT 1"));
    }

    default MemberMessageDO getFirstMessage(Long groupId){
        return selectOne(new LambdaQueryWrapperX<MemberMessageDO>()
                .eqIfPresent(MemberMessageDO::getGroupId, groupId)
                .orderByAsc(MemberMessageDO::getId).last("LIMIT 1"));
    }

    default boolean getRepeatCount(Long sendUserId, String content){
        if(StringUtils.isBlank(content) || content.length() < 10){
            return  false;
        }
        Long count = selectCount(new LambdaQueryWrapperX<MemberMessageDO>()
                .eq(MemberMessageDO::getSendUserId, sendUserId)
                .eq(MemberMessageDO::getContent, content));
        if(count == null){
            count = 0L;
        }
        return count > 1;
    }
}